package com.qf.myrbac.sys.controller;

import com.qf.myrbac.sys.entity.SysMenu;
import com.qf.myrbac.sys.entity.SysUser;
import com.qf.myrbac.sys.service.SysMenuService;
import com.qf.myrbac.utils.ResultMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author 徐英海
 * @since 2022-10-27
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
    @Autowired
    private SysMenuService menuService;

    @RequestMapping("/nav")
    public ResultMap nav() throws ClassNotFoundException {
        SysUser user = getUser();
        List<SysMenu> menuList = menuService.getUserMenuList(user.getUserId());
        return ResultMap.ok().put("menuList", menuList);
    }

    @RequestMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public List<SysMenu> list() {
        List<SysMenu> menus = menuService.list();
        Map<Long, String> map = new HashMap<>();
        menus.forEach(
                menu ->
                        map.put(menu.getMenuId(), menu.getName())
        );
        menus.forEach(
                menu ->
                        menu.setParentName(map.get(menu.getParentId()))
        );
        return menus;
    }
}
